//
//  CollectViewController.m
//  B
//
//  Created by Sunpeng on 15/11/16.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBaseHandle.h"
#import "MyCollectTableViewCell.h"
#import "PhotoDetailViewController.h"
@interface CollectViewController () <UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,retain)NSMutableArray *imageArray;
@end

@implementation CollectViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
    if (self.array.count == 0 && self.imageArray.count == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前没有收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }
    
    [super viewWillAppear:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的收藏";
    NSDictionary *dict=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTableView) name:@"reloadTableView" object:nil];
}
-(void)reloadTableView
{
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    self.imageArray = [[DataBaseHandle ShareDataBaseHandle]selectAllImageCollect];
    return self.imageArray.count;
}

//编辑
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    if (self.editing) {
        self.editButtonItem.title=@"完成";
    }else{
        self.editButtonItem.title=@"编辑";
    }
    
    [self.tableView setEditing:editing animated:YES];
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
       
        
            MyCollectModel *collectM = [self.imageArray objectAtIndex:indexPath.row];
            [[DataBaseHandle ShareDataBaseHandle]deleteImageCollectTable:collectM];
        
        
    }
    
    [self.tableView reloadData];
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyCollectModel *collectM = [self.imageArray objectAtIndex:indexPath.row];
    MyCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[MyCollectTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reuse"];
    }
    cell.titleLabel.text = collectM.textStr;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:collectM.picStr]];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
   
        MyCollectModel *collectM = [self.imageArray objectAtIndex:indexPath.row];
        PhotoDetailViewController *photoVC = [[PhotoDetailViewController alloc]init];
        photoVC.bId = collectM.idString;
        photoVC.type = (NSNumber *)collectM.typeString;
        photoVC.index = [collectM.index integerValue];
        photoVC.count = collectM.count;
        photoVC.titleText = collectM.textStr;
        NSLog(@"index = %@ bid = %@ type = %@ count = %ld",collectM.index,photoVC.bId,photoVC.type,collectM.count);
     [photoVC setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:photoVC animated:NO];
        
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0.12*HIGHT;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
